package com.langsin.blog.controller;

import com.langsin.blog.service.ArticleService;
import com.langsin.blog.service.impl.ArticleServiceImp;
import com.langsin.blog.util.JsonSerializeUtil;
import com.langsin.blog.util.MsgUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("article")
public class ArticleController {

    private ArticleService articleService = new ArticleServiceImp();
    private String msg;

    @RequestMapping(value = "save",method = RequestMethod.POST,params = {"title","brief","content","account"})
    public void articleSave(@RequestParam("account")String account,@RequestParam("brief")String brief, @RequestParam("title")String title,
                            @RequestParam("content")String content, HttpServletResponse response){
        msg = String.valueOf(articleService.articleSave(account,title,brief,content));
        MsgUtil.sendMsgToClient(msg,response);
    }

    @RequestMapping(value = "del",method = RequestMethod.POST,params = {"id"})
    public void articleDel(@RequestParam("id")String id,HttpServletResponse response){
        msg = String.valueOf(articleService.articleDel(id));
        MsgUtil.sendMsgToClient(msg, response);
    }

    @RequestMapping(value = "modify",method = RequestMethod.POST,params = {"id","prop","value"})
    public void articleModify(@RequestParam("id")String id,
                              @RequestParam("prop")String prop,@RequestParam("value")String value,HttpServletResponse response){
        msg = String.valueOf(articleService.articleModify(id,prop,value));
        MsgUtil.sendMsgToClient(msg, response);
    }

    @RequestMapping(value = "getArticle",method = RequestMethod.GET,params = {"articleId"})
    public void articleGet(@RequestParam("articleId")String articleId,HttpServletResponse response){
        msg = JsonSerializeUtil.convertToJsonString(articleService.articleGet(articleId));
        MsgUtil.sendMsgToClient(msg,response);
    }

    @RequestMapping(value="getArticleList",method = RequestMethod.GET,params = {"account"})
    public void articleListGet(@RequestParam("account")String account, HttpServletResponse response){
        msg = JsonSerializeUtil.convertToJsonString(articleService.articleListGet(account));
        MsgUtil.sendMsgToClient(msg,response);
    }
}